#!/usr/bin/perl -w
use strict;

my $file_dir = $ARGV[0];

# Read Test PMID-Journal association
my %pmid_journal;
open (FD, "results/Test_PMID_Journal") ||
	die ("Unable to open: results/Test_PMID_Journal\n");
while (<FD>){ 
	chop;
	my ($pmid, $journal) = split (/\,/, $_);
	$pmid_journal{$pmid} = $journal;
}
close (FD);

opendir(DIR, $file_dir) ||
	die ("Unable to open: $file_dir\n");
my @files = grep {(!/^\./)} readdir(DIR);
closedir (DIR);

for (my $k = 0; $k < scalar(@files); $k++){ 
	&extract_methods ($files[$k]);	
}

sub extract_methods{ 
	my ($file) = @_;
	
	my $id = $file;
	$id =~ s/\.txt//;
	if (not exists($pmid_journal{$id})){ 
		print "Not available: $id\n";
		exit;		
	}
	my $journal = $pmid_journal{$id};
	if ($journal eq "Proceedings of the National Academy of Sciences of the United States of America"){ 
		print "SKIPPING $id \n";
		return;
	}	

	my ($method_section_title, $next_section_title) = 
		&get_section_heads ($journal);
	
	my @next_patterns = split (/\,/, $next_section_title);
	my @method_patterns = split (/\,/, $method_section_title);

	# print "***************************\n";
 	# print $id . "\n";
 	# print $journal . "\n";
 	# print $method_section_title . "\n";
 	# print $next_section_title . "\n";
	my $flag_extraction = 0;
	my $method_text = "";
	my $count = 1;
	my $figure_text = "";
	
	my $path = $file_dir . "/" . $file;
	open (FD, $path) ||
		die ("Unable to open: $path\n");
	while (<FD>){ 
		chop;
	
		# print $_ . "\n";
		next if ($_ eq "");
		for (my $k = 0; $k < scalar(@method_patterns); $k++){
			# print "Checking " . $method_patterns[$k] . "\n";
			$method_patterns[$k] =~ s/\\\ /\\W/g;
			$method_patterns[$k] =~ s/^\ +//g;
			$method_patterns[$k] =~ s/\ +$//g;
			if ($_ =~ /$method_patterns[$k]/i){ 
				$flag_extraction = 1;
				$_ =~ s/^.*\ $method_patterns[$k]//; ;
				next;	
			}
		}

		next if ($flag_extraction == 0);

		for (my $k = 0; $k < scalar(@next_patterns); $k++){
			$next_patterns[$k] =~ s/\\\ /\\W/g;
			$next_patterns[$k] =~ s/^\ +//g;
			$next_patterns[$k] =~ s/\ +$/\\W/g;
			if ($_ =~ /\b$next_patterns[$k]\b/){ 
				# print "Found $next_patterns[$k]\n";
				&print_method ($id, $method_text, $figure_text);
				$flag_extraction = 2;
				close (FD);
				return;
			}
		}
	
		next if ($_ eq "JOURNAL OF BIOLOGICAL CHEMISTRY" or $_ =~ /^Downloaded from/ or 
			$_ =~ /^[1-9][0-9]*$/ or $_ =~ /Supplemental Material can be found at/);
	
		if ($_ =~ /^FIGURE/){ 
			$figure_text .= " " . $_;
		} else { 
			$method_text .= $count . "\t" . $_ . "\n";;
			$count++;
		}
	}
	close (FD);

	if ($flag_extraction == 0){ 
		print "SKIPPING " . $id . "#" . $journal . "\n";	
	}

	if ($flag_extraction == 1){ 
		&print_method ($id, $method_text, $figure_text);
	}
}

sub print_method{ 
	my ($id, $method_text, $figure_text) = @_;
	
	open (WR, ">preprocessing/methods/" . $id . ".txt"); 
	# print "<METHOD>\n";
	print WR $method_text . "\n";
	# print "</METHOD>\n";
	# print "<FIGURE>\n";
	# print $figure_text . "\n";
	# print "</FIGURE>\n";
	close (WR);
}

sub get_section_heads{ 
	
	my ($journal) = @_;
	my $methods = "";	
	my $next_sect = "";	

	# Molecular and cellular biology, MATERIALS AND METHODS # RESULTS # DISCUSSIONS # ACKNOWLEDGMENTS # REFERENCES # FIG.
	# The EMBO journal, Introduction # Results # Discussion # Materials and methods # Supplementary data # Acknowledgements # References
	# EMBO reports, INTRODUCTION # RESULTS AND DISCUSSION # METHODS # ACKNOWLEDGEMENTS # CONFLICT OF INTEREST # REFERENCES
	# Proceedings of the National Academy of Sciences of the United States of America, Results#Discussion#Materials and Methods # Refences start with numbers 1. # ACKNOWLEDGMENTS
	# Developmental cell, INTRODUCTION # RESULTS # DISCUSSION # EXPERIMENTAL PROCEDURES # Supplementary Material # Acknowledgments # REFERENCES
	# The Journal of biological chemistry, abbreviations used are:#MATERIALS AND METHODS#FIGURE#RESULTS#DISCUSSION#Supplemental Material#Acknowledgment#REFERENCES
	# Journal of molecular biology, Abstract # Keywords # Introduction # Results # Discussion # Methods # Acknowledgements # References
	# Molecular biology of the cell,INTRODUCTION#MATERIALS AND METHODS#RESULTS#DISCUSSION#ACKNOWLEDGMENTS#REFERENCES
	# Molecular cell,Summary#Keywords#Introduction#Results#Discussion#Experimental Procedures#Supplemental Data#Acknowledgments#References

	if ($journal eq "Molecular and cellular biology") { 
		$methods = "MATERIALS AND METHODS";
		$next_sect = "RESULTS";
	} elsif ($journal eq "The EMBO journal") { 
		$methods = "Materials and methods";
		$next_sect = "Supplementary data, Acknowledgements, References";
	} elsif ($journal eq "EMBO reports") { 
		$methods = "METHODS";
		$next_sect = "ACKNOWLEDGEMENTS, CONFLICT OF INTEREST,  REFERENCES";
	} elsif ($journal eq "Proceedings of the National Academy of Sciences of the United States of America") { 
		$methods = "Materials and Methods";
		$next_sect = "1.";
	} elsif ($journal eq "Developmental cell") { 
		$methods = "EXPERIMENTAL PROCEDURES, MATERIALS AND METHODS";
		$next_sect = "Supplementary Material, Acknowledgments, REFERENCES";
	} elsif ($journal eq "The Journal of biological chemistry") { 
		$methods = "MATERIALS AND METHODS, EXPERIMENTAL PROCEDURES";
		$next_sect = "RESULTS";
	} elsif ($journal eq "Journal of molecular biology") { 
		$methods = "Methods, Material and methods";
		$next_sect = "Acknowledgements, References";
	} elsif ($journal eq "Molecular biology of the cell") { 
		$methods = "MATERIALS AND METHODS";
		$next_sect = "RESULTS";
	} elsif ($journal eq "Molecular cell") { 
		$methods = "Experimental Procedures, EXPERIMENTAL PROCEDURES";
		$next_sect = "Supplemental Data, Acknowledgments, References, Supplementary Material, ACKNOWLEDGEMENTS";
	}
		
	return ($methods, $next_sect);
}
